package com.guit.hrms.mapper;

import com.guit.hrms.dto.interview.UpdateInterviewStatusDTO;
import com.guit.hrms.po.interview.Interviewee;
import com.guit.hrms.po.interview.Offer;
import com.guit.hrms.vo.IntervieweeVO;
import com.guit.hrms.vo.OfferVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface IntervieweeMapper {
    /**
     * 应聘者上传简历
     * @param interviewee 应聘者信息
     * @return 结果
     */
    @Insert("insert into interviewee(name, email, phone, resume, plan_id, dept_id, position_number, identification_number) values (#{name}, #{email}, #{phone}, #{resume}, #{planId}, #{deptId}, #{positionNumber}, #{identificationNumber})")
    Integer uploadResume(Interviewee interviewee);

    /**
     * 根据应聘者id查询应聘者信息
     * @param id 应聘者id
     * @return 应聘者信息
     */
    @Select("select * from interviewee where id=#{id}")
    Interviewee getIntervieweeById(Integer id);

    /**
     * 查询应聘者信息
     *
     * @param planId         招聘计划id
     * @param deptId         应聘部门id
     * @param positionNumber 应聘岗位编号
     * @return 应聘者信息列表
     */
    List<IntervieweeVO> getInterviewee(Integer planId, String deptId, String positionNumber);

    /**
     * 修改应聘者面试状态
     * @param updateInterviewStatusDTO 面试状态信息
     * @return Result
     */
//    @Update("update interviewee set interview_status=#{interviewStatus} where id=#{intervieweeId}")
    Integer updateInterviewStatus(UpdateInterviewStatusDTO updateInterviewStatusDTO);

    /**
     * 删除应聘者信息
     * @param intervieweeId 应聘者id
     * @return 结果
     */
    @Delete("delete from interviewee where id=#{intervieweeId}")
    Integer deleteInterviewee(Integer intervieweeId);

    /**
     * 为应聘者申请offer
     * @param offer offer信息
     * @return 结果
     */
    @Insert("insert into offer(interviewee_id, instructions, applicant_id, create_time, update_time) values (#{intervieweeId}, #{instructions}, #{applicantId}, #{createTime}, #{updateTime})")
    Integer applyOfferForInterviewee(Offer offer);

    /**
     * 根据应聘者id查询offer信息
     * @param intervieweeId 应聘者id
     * @return offer信息
     */
    @Select("select * from offer where interviewee_id=#{intervieweeId}")
    Offer getOfferByIntervieweeId(Integer intervieweeId);

    /**
     * 更新应聘者试用期及面试状态
     * @param id 应聘者id
     * @param tryoutStartTime 试用期开始时间
     * @param tryoutEndTime 试用期结束时间
     * @param tryoutDuration 试用期时长
     * @return 结果
     */
    @Update("update interviewee set tryout_start_time = #{tryoutStartTime}, tryout_end_time = #{tryoutEndTime}, tryout_duration = #{tryoutDuration}, interview_status = 6 where id=#{id}")
    Integer updateTryout(Integer id, LocalDate tryoutStartTime, LocalDate tryoutEndTime, String tryoutDuration);

    /**
     * 根据应聘者id获取部门/学院主键
     * @param intervieweeId
     * @return
     */
    @Select("select plan_id,dept_id,position_number from interviewee where id = #{intervieweeId}")
    Interviewee getPrimaryKey(Integer intervieweeId);

    /**
     * 查询是否在黑名单
     * @param identificationNumber 身份证号
     * @return 应聘者信息
     */
    @Select("select * from interviewee where interview_status = 10 and identification_number = #{identificationNumber}")
    Interviewee selectBlacklistIntervieweeByIdentificationNumber(String identificationNumber);

    /**
     * 查询重复的记录
     * @param interviewee 应聘者信息
     * @return 结果
     */
    @Select("select * from interviewee where identification_number=#{identificationNumber} and plan_id = #{planId} and dept_id = #{deptId} and position_number = #{positionNumber}")
    Interviewee selectRepeatInterviewee(Interviewee interviewee);

    /**
     * 根据offerToken修改result
     * @param offerToken offerToken
     * @param result 是否接受
     */
    @Update("update offer set result = #{result} , offer_token = '已使用' where offer_token = #{offerToken}")
    Integer updateOfferTokenByResult(String offerToken, Integer result);

    /**
     * 根据时间范围和结果查询offer信息
     *
     * @param startTime
     * @param endTime
     * @param result
     * @return
     */
    List<OfferVO> getOfferResult(LocalDateTime startTime, LocalDateTime endTime, Integer result);

    /**
     * 查询所有未选择是否接受offer的记录
     * @return 结果
     */
    @Select("select * from offer where result = 0 and expiry_time > #{now} ORDER BY expiry_time ")
    List<Offer> findAllActiveOffers(LocalDateTime now);

    /**
     * 获取所有待接受且已过期的offer
     * @param now 当前时间
     * @param pageSize 分页大小
     * @param offset 偏移量
     * @return offer列表
     */
    @Select("SELECT * FROM offer WHERE result = 0 AND expiry_time < #{now} ORDER BY id LIMIT #{pageSize} OFFSET #{offset}")
    List<Offer> checkExpiredOffersBatch(LocalDateTime now, int pageSize, int offset);

    /**
     * 批量更新offer状态
     * @param ids offer id列表
     * @param result offer状态
     * @return 更新结果
     */
    Integer updateOfferResult(List<Integer> ids, int result);

    /**
     * 根据id查询offer信息
     * @param id offer id
     * @return offer信息
     */
    @Select("select * from offer where id = #{id}")
    Offer getOfferById(Integer id);

    /**
     * 根据应聘者id删除offer信息
     * @param intervieweeId 应聘者id
     * @return 删除结果
     */
    @Delete("delete from offer where interviewee_id = #{intervieweeId}")
    Integer deleteOfferByIntervieweeId(Integer intervieweeId);
}
